package com.henry.db.migrate.util;

import java.io.File;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class CommonUtil {
	/**
	 * 获取本机ip
	 * @return 本机ip
	 */
	public static String getLocalIp() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取相对路径的URL
	 * @param relativePath
	 * @return 相对路径的URL
	 */
	private static URL getCommonURL(String relativePath){
		URL url = Thread.currentThread().getContextClassLoader().getResource(relativePath);
		if(url==null){
			url = CommonUtil.class.getClassLoader().getResource(relativePath);
			if(url ==null){
				try{
				url = new File(relativePath).toURI().toURL();
				}catch(MalformedURLException e){
					e.printStackTrace();
				}
			}
		}
		
		return url;
	}
	
	/**
	 * 获取工程根路径URL的 
	 * @return
	 */
	public static String getRootPath(){
		URL url = getCommonURL("");
		String path = url.getPath();
		return path;
	}
	public static String getRelativePath(String relativePath){
		URL url = getCommonURL(relativePath);
		String path = url.getPath();
		return path;
	}
}
